# _*_ coding:utf-8 _*_
# @File  : data_utils.py
# @Time  : 2022-07-08
# @Author: zizle
import numpy as np
import pandas as pd


def list2tree(data: list, pkey: str, ckey: str,  fvalue: str):
    # pkey: parent key
    # ckey: compare key
    # fvalue: first value
    # list2tree(data, pkey, ckey, menu[ckey])
    return [dict(menu, **{'children': list2tree(data, pkey, ckey, menu[ckey])}) for menu in
            [m for m in data if str(m[pkey]) == str(fvalue)]]


def datetime_column_converter(source_time):
    try:
        return pd.to_datetime(source_time)
    except ValueError as e:
        print(e)
        return np.NAN
